<?php
/**
 * Created by PhpStorm.
 * User: zxd
 * Date: 2016/12/31 0031
 * Time: 下午 8:49
 */

namespace p5\objects;


class ProxyUtils
{

    public static function showResource($url)
    {
        $type = FileUtils::get_file_type($url);
        if (self::isImage($type)) {
            self::showImage($url);
            return;
        }
        if (in_array($type, ["html", "htm"])) {
            self::showImage($url);
            return;
        }
        self::showText($url, "text/plain");
    }


    public static function showImage($url)
    {
        $type = FileUtils::get_file_type($url);
        if (empty($type) || !self::isImage($type)) {
            $type = "jpg";
        }
        @header("Content-Type:image/$type");
        $im = imagecreatefromjpeg($url);
        $width = imagesx($im);
        $height = imagesy($im);
        $x = $width / 1;
        $y = $height / 1;
        $dst = imagecreatetruecolor($x, $y);
        imagecopyresampled($dst, $im, 0, 0, 0, 0, $x, $y, $width, $height);
        imagejpeg($dst);
        imagedestroy($dst);
        imagedestroy($im);
    }


    public static function getSSLPage($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSLVERSION,3);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    public static function showText($url, $contentType = "text/html")
    {

    }

    /**
     * @param $type
     * @return bool
     */
    public static function isImage($type)
    {
        return in_array($type, ["jpg", "jpeg", "png", "gif"]);
    }

}